<?php

namespace app\api\controller;

use app\api\model\CircleCommit;
use app\api\model\CircleLike;
use app\api\model\Member as MemberModel;
use app\api\model\Circle as CircleModel;
use app\api\model\CircleImage;
use think\facade\Filesystem;

class Circle extends Member
{
    public function __construct()
    {
        parent::__construct();
    }

    public function add()
    {
        $content = input('get.content');
        $address = input('get.address');
        if (empty($content)) output_error('请输入内容');
        $data = array();
        $data['member_id'] = $this->memberId;
        $data['circle_content'] = $content;
        if (!empty($address)) $data['circle_address'] = $address;
        $data['visit_number'] = 0;
        $data['like_number'] = 0;
        $data['commit_number'] = 0;
        $data['add_time'] = time();
        $id = CircleModel::insertGetId($data);
        output_data($id);
    }

    public function getList()
    {
        //分页 page 函数 limit
        $page = input('get.page');
        $data = CircleModel::page($page)->limit(20)->order('circle_id desc')->select();
        foreach ($data as $value) {
            //是否点赞
            $condition = array();
            $condition[] = ['circle_id', '=', $value['circle_id']];
            $condition[] = ['member_id', '=', $this->memberId];
            $likeData = CircleLike::where($condition)->find();
            $value['is_like'] = empty($likeData) ? '0' : '1';
            //添加用户数据
            $memberData = MemberModel::where('member_id', '=', $value['member_id'])
                ->field('member_nickname,member_avatar')->find();
            $memberData['member_avatar'] = URL_AVATAR . $memberData['member_avatar'];
            $value['member_data'] = $memberData;
            //添加图片数据
            $imageData = CircleImage::where('circle_id', '=', $value['circle_id'])->select();
            foreach ($imageData as $image) {
                $image['image_url'] = URL_CIRCLE . $image['image_name'];
            }
            $value['image_data'] = $imageData;
            //点赞数据
            $likeData = CircleLike::where('circle_id', '=', $value['circle_id'])->select();
            foreach ($likeData as $like) {
                $like['member_nickname'] = MemberModel::where('member_id', '=', $like['member_id'])
                    ->field('member_nickname')->find()['member_nickname'];
            }
            $value['like_data'] = $likeData;
            //评论数据
            $commitData = CircleCommit::where('circle_id', '=', $value['circle_id'])->select();
            foreach ($commitData as $commit) {
                $commit['member_nickname'] = MemberModel::where('member_id', '=', $commit['member_id'])
                    ->field('member_nickname')->find()['member_nickname'];
                $replyMemberNickname = '';
                if ($commit['reply_id'] != '-1') {
                    //代表此条有回复上一条评论，上一条 commit_id 就是 reply_id
                    //找到上一条评论的数据
                    $replyData = CircleCommit::where('commit_id', '=', $commit['reply_id'])->find();
                    $replyMemberNickname = MemberModel::where('member_id', '=', $replyData['member_id'])
                        ->field('member_nickname')->find()['member_nickname'];
                }
                $commit['reply_member_nickname'] = $replyMemberNickname;
            }
            $value['commit_data'] = $commitData;
        }
        output_data($data);
    }

    //上传图片
    public function uploadImage()
    {
        //第一步：上传文件拿到文件名
        $file = request()->file('image');
        $name = Filesystem::disk('circle')->putFile('', $file, 'uniqid');
        //第二步：写入到circle_image
        $data = array();
        $data['circle_id'] = input('get.id');
        $data['image_name'] = $name;
        $data['add_time'] = time();
        CircleImage::insert($data);
        output_data(1);
    }

    //点赞操作
    public function like()
    {
        //第一步：校验接收的数据
        $id = input('get.id');
        if (empty($id)) output_error('参数错误');
        //第二步：判断此条动态是否存在
        $data = CircleModel::where('circle_id', '=', $id)->find();
        if (empty($data)) output_error('此条动态不存在');
        //第三步：是否已经点赞
        $condition = array();
        $condition[] = ['circle_id', '=', $id];
        $condition[] = ['member_id', '=', $this->memberId];
        $data = CircleLike::where($condition)->find();
        if (!empty($data)) output_error('您已经点过赞了');
        //第三步：点赞操作
        $data = array();
        $data['circle_id'] = $id;
        $data['member_id'] = $this->memberId;
        $data['add_time'] = time();
        CircleLike::insert($data);
        output_data(1);
    }

    //取消点赞操作
    public function unlike()
    {
        //第一步：校验接收的数据
        $id = input('get.id');
        if (empty($id)) output_error('参数错误');
        //第二步：判断此条动态是否存在
        $data = CircleModel::where('circle_id', '=', $id)->find();
        if (empty($data)) output_error('此条动态不存在');
        //第三步：是否已经点赞
        $condition = array();
        $condition[] = ['circle_id', '=', $id];
        $condition[] = ['member_id', '=', $this->memberId];
        $data = CircleLike::where($condition)->find();
        if (empty($data)) output_error('你尚未点赞，无法取消');
        //第三步：取消点赞操作
        CircleLike::where($condition)->delete();
        output_data(1);
    }

    //发表评论
    public function commit()
    {
        //第一步：校验接收的数据
        $id = input('get.id');
        $replyId = input('get.reply_id');
        $content = input('get.content');
        if (empty($id)) output_error('参数错误');
        if (empty($content)) output_error('参数错误');
        //第二步：判断此条动态是否存在
        $data = CircleModel::where('circle_id', '=', $id)->find();
        if (empty($data)) output_error('此条动态不存在');
        //第三步：发表评论
        $data = array();
        $data['circle_id'] = $id;
        $data['member_id'] = $this->memberId;
        $data['reply_id'] = empty($replyId) ? '-1' : $replyId;
        $data['commit_content'] = $content;
        $data['add_time'] = time();
        CircleCommit::insert($data);
        output_data(1);
    }

    //删除动态
    public function delete()
    {
        //第一步：校验接收的数据
        $id = input('get.id');
        if (empty($id)) output_error('参数错误');
        //第二步：判断此条评论是否存在
        $data = CircleModel::where('circle_id', '=', $id)->find();
        if (empty($data)) output_error('此条动态不存在');
        //第三步：判断是否有权限删除
        if ($data['member_id'] != $this->memberId) output_error('无权操作');
        //第四步：正式删除
        CircleLike::where('circle_id', '=', $id)->delete();
        CircleCommit::where('circle_id', '=', $id)->delete();
        $image = CircleImage::where('circle_id', '=', $id)->select();
        foreach ($image as $i) {
            $filename = app()->getRootPath() . 'public\\upload\\images\\circle\\' . $i['image_name'];
            unlink($filename);
        }
        CircleImage::where('circle_id', '=', $id)->delete();
        CircleModel::where('circle_id', '=', $id)->delete();
        output_data(1);
    }

    //删除评论、
    public function deleteCommit()
    {
        //第一步：校验接收的数据
        $id = input('get.id');
        if (empty($id)) output_error('参数错误');
        //第二步：判断此条评论是否存在
        $data = CircleCommit::where('commit_id', '=', $id)->find();
        if (empty($data)) output_error('此条评论不存在');
        //第三步：判断是否有权限删除
        if ($data['member_id'] != $this->memberId) output_error('无权操作');
        //第四步：删除评论
        CircleCommit::where('commit_id', '=', $id)->delete();
        output_data(1);
    }

}
